/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.cmd;

import java.util.ArrayList;
import java.util.List;

import self.amigo.diag.layer.BasicDiagramLayer;
import self.gee.IDiagramLayer;
import self.gee.IGraphicElement;
import self.gee.editor.ICommand;
import self.gee.editor.IDiagramEditor;

public class SelectionForwardBackPositioning implements ICommand {
  public static final int TO_FRONT = 0;
  public static final int MOVE_FORWARD = 1;
  public static final int TO_BACK = 2;
  public static final int MOVE_BACKWORD = 3;

  private IDiagramEditor  editor;
  private List            index = new ArrayList();
  private List            selection = new ArrayList();
  private int             type;
  
  public SelectionForwardBackPositioning( IDiagramEditor ed, List s, int t ) {
    editor = ed;
    selection.addAll( s );
    type = t;
    indexSelection();
  }
  
  public void execute() {
    IDiagramLayer layer = editor.getSource().getContext();
    List refreshedElements = SelectionUtils.toNewElementList( selection, layer );
    layer.beginUpdate();
    int max = selection.size()-1;
    for ( int cntr = max; cntr >= 0; cntr-- ) {
      IGraphicElement elem = (IGraphicElement) refreshedElements.get( cntr );
      switch ( type ) {
        case TO_FRONT: layer.toFront( elem ); break;
        case TO_BACK: layer.toBack( elem ); break;
        case MOVE_FORWARD: layer.moveForward( elem ); break;
        case MOVE_BACKWORD: layer.moveBackward( elem ); break;
        default:
          throw new IllegalStateException( "err.type.positioning.invalid" );
      }
    }
    layer.endUpdate();
    editor.setSelectedElements( refreshedElements );
    editor.getSource().redraw();
  }

  public void undo() {
    IDiagramLayer layer = editor.getSource().getContext();
    List refreshedElements = SelectionUtils.toNewElementList( selection, layer );

    List allElems = new ArrayList();
    layer.getElements( allElems ); 
    
    int max = selection.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      Object elem = refreshedElements.get( cntr );
      Integer idx = (Integer) index.get( cntr );
      allElems.remove( elem );
      allElems.add( idx.intValue(), elem );
    }
    
    layer.beginUpdate();
    ((BasicDiagramLayer) layer).assignElements( allElems );
    layer.endUpdate();

    editor.setSelectedElements( refreshedElements );
    editor.getSource().redraw();
  }
  
  private void indexSelection() {
    List allElems = new ArrayList();
    editor.getSource().getContext().getElements( allElems ); 
    int max = selection.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      Integer idx = new Integer( allElems.indexOf(selection.get(cntr)) );
      index.add( idx );
    }
  }
}